package cn.richard.demo;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.richard.demo.entity.MbsTestEntity;
import cn.richard.demo.mapper.MbsTestEntityMapper;

public class MainTest {
	public static void main(String[] args) throws IOException {
		//1 获取 SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		
		//2 获取 SqlSession
		//JDK7 自动关闭资源语法
		try (SqlSession session = sqlSessionFactory.openSession()) {
			//3 获取 Mapper 代理对象
			MbsTestEntityMapper mapper = session.getMapper(MbsTestEntityMapper.class);
			
			//4 mapper 方法执行
			MbsTestEntity selectByPrimaryKey = mapper.selectByPrimaryKey(1L);
			System.out.println(selectByPrimaryKey);
		}
	}

	
	private static SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;
	}
}
